<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>车票预订 - 列车车次管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/styles.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <div class="container">
        <a class="navbar-brand" href="/">
            <i class="fas fa-train me-2"></i>列车车次管理系统
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/">首页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="/user/booking">车票预订</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" id="myTicketsLink" href="/user/tickets">我的车票</a>
                </li>
            </ul>
            <ul class="navbar-nav" id="authNav">
                <!-- 动态填充登录状态 -->
            </ul>
        </div>
    </div>
</nav>

<div class="container mt-4">
    <div class="card mb-4">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">车票查询</h5>
        </div>
        <div class="card-body">
            <form id="searchForm" class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">出发站</label>
                    <select class="form-select" id="startStation">
                        <!-- 动态加载车站 -->
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">到达站</label>
                    <select class="form-select" id="endStation">
                        <!-- 动态加载车站 -->
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">出发日期</label>
                    <input type="date" class="form-control" id="departureDate">
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">查询</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0" id="resultHeader">车次信息</h5>
        </div>
        <div class="card-body">
            <div id="loadingSpinner" class="text-center py-5 d-none">
                <div class="spinner-border text-primary" role="status">
                    <span class="visually-hidden">Loading...</span>
                </div>
                <p class="mt-2">正在加载车次信息...</p>
            </div>

            <div id="noResultMsg" class="alert alert-info d-none">
                没有找到符合条件的车次，请调整查询条件后重试。
            </div>

            <div class="table-responsive">
                <table class="table table-hover" id="scheduleTable">
                    <thead>
                    <tr>
                        <th>车次</th>
                        <th>出发站</th>
                        <th>到达站</th>
                        <th>出发时间</th>
                        <th>到达时间</th>
                        <th>历时</th>
                        <th>二等座</th>
                        <th>一等座</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <!-- 动态加载车次数据 -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 车票预订模态框 -->
<div class="modal fade" id="bookingModal" tabindex="-1" aria-labelledby="bookingModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="bookingModalLabel">预订车票</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="bookingForm">
                    <input type="hidden" id="scheduleId">

                    <div class="mb-3">
                        <h6>车次信息</h6>
                        <p id="trainInfo" class="text-primary"></p>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">座位类型</label>
                        <select class="form-control" id="seatClass" required>
                            <option value="二等座">二等座</option>
                            <option value="一等座">一等座</option>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">乘客姓名</label>
                        <input type="text" class="form-control" id="passengerName" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">身份证号</label>
                        <input type="text" class="form-control" id="idCard" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">联系电话</label>
                        <input type="tel" class="form-control" id="phone" required>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="confirmBooking">确认预订</button>
            </div>
        </div>
    </div>
</div>

<footer class="bg-light text-center text-lg-start mt-5">
    <div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.05);">
        © 2025 列车车次管理系统 -
        <a class="text-dark" href="/">upup.com</a>
    </div>
</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="/js/auth.js"></script>
<script>
    $(document).ready(function() {
        // 更新导航栏认证状态
        updateAuthNav();
        
        // 检查用户是否已登录，如未登录则重定向
        getCurrentUser().catch(function() {
            window.location.href = '/custom-login?redirect=' + encodeURIComponent(window.location.pathname);
        });
    });
</script>
</body>
</html>